<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\facade\View;
use think\Request;
use think\Session;
use Qiniu\Storage\UploadManager;
use Qiniu\Auth;
class Week extends Controller
{
    public function user(Request $request)
    {
        $data=$request->post();
        $data['create_time']=time();
        $res=Db::name('user')->insert($data);
        if ($res){
            \session('id',$res['id']);
            return json(['code'=>200,'msg'=>'用户添加成功','data'=>$data]);
        }else{
            return json(['code'=>200,'msg'=>'用户添加失败','data'=>null]);
        }
    }
    public function login(Request $request)
    {
        $data=$request->post();
        $uid=\session('id',$data['id']);
        if (Db::name('user')->where($uid)->find()){
            return json(['code'=>200,'msg'=>'登录成功','data'=>$data]);
        }else{
            return json(['code'=>200,'msg'=>'登录失败','data'=>null]);
        }
    }

    public function create()
    {
        return View::fetch();
    }

    public function add(Request $request)
    {
       $data=$request->post();
       $data['create_time']=time();
       //图片上传
        $file = request()->file('img');
        // 移动到框架应用根目录/uploads/ 目录下
        $info = $file->move( './uploads');
        if($info){
            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
           $img1=$info->getSaveName();
            // 输出 42a79759f284b767dfcb2a0197904287.jpg
            $img2=$info->getFilename();
        }else{
            // 上传失败获取错误信息
            echo $file->getError();
        }
        $images='/uploads/'.$img1;
        $str=substr($img1,0,8);
        //图片处理
      $image1 = \think\Image::open('.'.$images);
// 按照原图的比例生成一个最大为150*150的缩略图并保存为text.png
      $thumb=$image1->thumb(150, 150)->text('测试',getcwd().'./simfang.ttf',20,'#ff0000',1)->save('./uploads/'.$str.'/'.time().'_text.jpg');
      $images1='./uploads/'.$str.'/'.time().'_text.jpg';
      $data['img']=$images1;
      if (Db::name('photo')->insert($data)){
          //上传七牛云
          $uploadMgr = new UploadManager();
          $accessKey='rDo22qVaCK2rhJXQM9Ez0IxYzYOA383mT87_viap';
          $secretKey='m3zQ7o-rURBQc3Neufx3ptjJiJnMpVJstXXX1PuT';
          $bucket='fengbaobao';
          $auth = new Auth($accessKey, $secretKey);
          $token = $auth->uploadToken($bucket);
          list($ret, $error) = $uploadMgr->putFile($token, $img2, $images1);
          return $this->success('上传成功','show');
      }else{
          return $this->error('上传失败','create');
      }
    }

//展示
    public function show(Request $request)
    {
        $where=$request->post('where');
        if ($where){
            $data['img']=$request->post('img');
            $where[]=['class'=>$data['img']];
            $data=Db::name('photo')->where($where)->paginate('2');
            return View::fetch('show',['data'=>$data]);
        }else{
            $data=Db::name('photo')->paginate('2');
            return View::fetch('show',['data'=>$data]);
        }

  }



}
